package com.bootdo.common.rabbitmqconfig;

import com.bootdo.common.redisUtils.ObjectUtils;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyOrderAll;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyOrderAllService;
import com.front.web.lvyou_specialty_front.pagevo.GoodsContentBodyVo;
import com.front.web.lvyou_specialty_front.pagevo.GoodsOrderVo;
import com.front.web.lvyou_specialty_front.util.OrderContentStatus;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqLvyouConfig {


    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Autowired
    private ConnectionFactory connectionFactory;

    @Autowired
    private LvyouSpecialtyOrderAllService lvyouSpecialtyOrderAllService;

    @Bean
    FanoutExchange contractMyFanoutExchange() {
        FanoutExchange fanoutExchange = new FanoutExchange("com.timeout.myfanout",true,true);
        this.rabbitAdmin.declareExchange(fanoutExchange);
        return fanoutExchange;
    }

    // 延时队列
    @Bean
    Queue myTimeOutQueue() {
        Queue queue = new Queue("com.lxj.mytimeoutqueue", true);
        this.rabbitAdmin.declareQueue(queue);
        return queue;
    }

    // 主队列
    @Bean
    Queue lxjMasterQueue() {

        Map args = new HashMap();
        //设置生存时间 6秒
        args.put("x-message-ttl" , 6000);
        //超时后移送到哪个交换器
        args.put("x-dead-letter-exchange", "com.timeout.myfanout");
        //超时移送到指定交换器的同时 制定key
        //args.put("x-dead-letter-routing-key","666ssss");
        Queue queue = new Queue("com.lxj.masterqueue", true,false,false, args);
        this.rabbitAdmin.declareQueue(queue);
        return queue;
    }

    // 将主队列绑定到topic上面
    @Bean
    public Binding bindingMyMaster() {
        return BindingBuilder.bind(lxjMasterQueue()).to(haojiangboTopicExchange()).with("*.lxjlvyou.*");
    }

    // 将其绑定到延时交换机上面
    @Bean
    public Binding bindingMyTimeOutQueue() {

        //     BindingBuilder.bind(haojiangboMasterQueue()).to(haojiangboTopicExchange()).with("*.haojiangbo.*");
        return  BindingBuilder.bind(myTimeOutQueue()).to(contractMyFanoutExchange());

    }

    /**
     * topIc队列
     * @return
     */
    @Bean
    TopicExchange haojiangboTopicExchange() {
        TopicExchange contractTopicExchange = new TopicExchange("com.haojiangbo.topic",true,true);
        this.rabbitAdmin.declareExchange(contractTopicExchange);
        return contractTopicExchange;
    }

    @Bean
    public SimpleMessageListenerContainer myMessageContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueues(myTimeOutQueue());
        container.setExposeListenerChannel(true);
        container.setMaxConcurrentConsumers(1);
        container.setConcurrentConsumers(1);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL); //设置确认模式手工确认
        container.setMessageListener(new ChannelAwareMessageListener() {
            @Override
            public void onMessage(Message message, Channel channel) throws IOException {

                try {
                    byte[] body = message.getBody();
                    Object unserialize = ObjectUtils.unserialize(body);
                    System.out.println(unserialize);
                    if (unserialize instanceof GoodsOrderVo) {
                        GoodsOrderVo goodsOrderVoInQueue = (GoodsOrderVo) ObjectUtils.unserialize(body);
                        LvyouSpecialtyOrderAll lvyouSpecialtyOrderAll = goodsOrderVoInQueue.getLvyouSpecialtyOrderAll();
                        lvyouSpecialtyOrderAll = lvyouSpecialtyOrderAllService.get(lvyouSpecialtyOrderAll.getId());

                        System.err.println("我的延时队列收到消息...");

                        if(null != lvyouSpecialtyOrderAll){
                            if (lvyouSpecialtyOrderAll.getStatus() != 2) {
                                lvyouSpecialtyOrderAll.setStatus(3);
                                if(lvyouSpecialtyOrderAllService.defferBack(lvyouSpecialtyOrderAll) > 0){
                                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                                    System.err.println("确认消息...");
                                    return;
                                }
                            }
                            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                        }
                    } else {
                        System.out.println("类型错误");
                    }

                    channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,true); //确认消息成功消费*//*
                }catch (Exception e){
                    System.err.println("重新入队...异常");
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false); //确认消息成功消费*//*
                    e.printStackTrace();
                }
            }
        });
        return container;
    }

}
